/* 自动生成导航栏 */
// docsify serve docs
const fs = require('fs');

function navbar() {
    let str = "* [归档](src\\archive.html)\n";
    let dirs = fs.readdirSync('books');
    dirs.forEach(file => {
        str += `* ${file}\n`;
        let dirs1 = fs.readdirSync('books/' + file);
        dirs1.forEach(i => {
            str += `  * [${i.replace('.md', '')}](books\\${file}\\${i})\n`;
        })
    })
    // console.log(str);
    fs.writeFileSync('_navbar.md', str);
}
function archive() {
    let archiveJson = [];
    let dirs = fs.readdirSync('books');
    dirs.forEach(file => {
        let list = [];
        fs.readdirSync('books/' + file).forEach(i => {
            let fileName = `books/${file}/${i}`;
            // 获取文件创建时间UTC,转化为北京时间   
            let birthtime = fs.statSync(fileName).birthtime;
            birthtime = new Date(new Date(birthtime).getTime() + 8 * 3600 * 1000);

            list.push({
                articleName: i.substring(0, i.lastIndexOf(".")),
                articleTime: birthtime
            })
        });
        archiveJson.push({
            title: file,
            // data: list.sort((a, b) => a.articleTime.localeCompare(b.articleTime))
            data: list.sort((a, b) => {

                return new Date(b.articleTime).getTime() - new Date(a.articleTime).getTime()
            })
        })
    })
    fs.writeFileSync('src/archive.json', JSON.stringify(archiveJson));
}

navbar();
archive();